/**
 * 信息修改
 */
define(['ldfw/core','text!tpl/user/profile_edit.tpl','art',
        'ldfw/ldlibs/dict','comps/ajax',
        'comps/session','ldfw/helper/formatHelper','city-picker'],
    function (LDFW,tpl,art,dict,ajax,session) {
        let View = LDFW.Container.extend({
            tpl: tpl,
            className: 'panel',
            initialize: function () {
                let curr_user = session.getCurrentUser()
                this.model.set(curr_user)
                this.on('shown', function(){
                    this.loadData()
                })
            },
            rendered:function(){
                this.$el.find('#address-picker').cityPicker({
                    title: "请选择地区"
                })
            },
            loadData:function(){
                const _this = this
                if(this.model.get('cust_type')=='1'){
                    ajax.post('pfL.3.107').success(function(resp){
                        let userInfo = resp.result[0]
                        _this.model.set(userInfo)
                        let address = userInfo.register_address
                        if(address.indexOf('  ')>0){
                            let address1 = address.split('  ')[0];
                            let address2 = address.split('  ')[1]
                            _this.model.set('address1',address1)
                            _this.model.set('address2',address2)
                        }else{
                            _this.model.set('address2',address)
                        }
                        _this.render()
                    })
                }else if(this.model.get('cust_type')=='2'){
                    ajax.post('pfL.3.106').success(function(resp){
                        let userInfo = resp.result[0]
                        _this.model.set(userInfo)
                        let address = userInfo.conta_addr
                        if(address.indexOf('  ')>0){
                            let address1 = address.split('  ')[0];
                            let address2 = address.split('  ')[1]
                            _this.model.set('address1',address1)
                            _this.model.set('address2',address2)
                        }else{
                            _this.model.set('address2',address)
                        }
                        _this.render()
                    })
                }
            },
            setTitle(title){
                this.title = title
                this.model.set('title',title)
            },
            save:function(){
                switch(this.title){
                    case 'address':
                        let contact_address1 = this.$el.find('#address-picker').val(),
                            contact_address2 = this.$el.find('#address-input').val(),
                            curr_user = session.getCurrentUser()
                        if(curr_user.cust_type==1){
                            ajax.post('pfL.3.129',{
                                register_address:contact_address1+'  '+contact_address2,
                                office_address:contact_address1+contact_address2
                            }).success(function(){
                                $.toast('操作成功',1000,function(){
                                    LDFW.navigate('#profile')
                                })
                            })
                        }else if(curr_user.cust_type==2){
                            ajax.post('pfL.3.128',{
                                conta_addr:contact_address1+'  '+contact_address2
                            }).success(function(){
                                $.toast('操作成功',1000,function(){
                                    LDFW.navigate('#profile')
                                })
                            })
                        }
                        break;
                }
            }
        })
        return new View()
    })